<?php

namespace XWAM\Addon\PXCallSystem\Controller;

/**
 * 用户控制器（包含管理员和座席）
 *
 * @author Alen
 * @since 1.0 2014-9-11 by sutroon
 */
class RechargeController extends AppbaseController {

    public function index() {
        $search_data = $this->recharge_search_data();
        $this->assign('search', $search_data['search']);
        $this->display();
    }

    public function recharge_search_data() {
        $search = I();
        if (!$this->isSuperAdmin) {
            $where['siteID'] = $this->loginData['siteID'];
        }
        // 默认显示一个月记录
        $where['state'] = array('in', '1,2,3');
        if (!$search['begindate']) {
            $search['begindate'] = date('Y-m-1');
        }
        if (!$search['enddate']) {
            $search['enddate'] = date('Y-m-d');
        }
        $where['createdTime'] = array(
            array('gt', $search['begindate'] . ' 00:00:00'),
            array('lt', $search['enddate'] . ' 23:59:59')
        );
        if ($search['origin']) {
            $where['origin'] = $search['origin'];
        }
        if ($search['siteID']) {
            $where['siteID'] = $search['siteID'];
        }
        return array('search' => $search, 'where' => $where);
    }

    public function recharge_export() {
        $search_data = $this->recharge_search_data();
        $list = AddonD('Recharge')->scope('export')->where($search_data['where'])->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    /**
     * 显示项目列表
     */
    public function get_recharge_list_json() {
        $search_data = $this->recharge_search_data();
        $list = AddonD('Recharge')->get_recharge_list($search_data['where']);
        $this->ajaxReturn($list);
    }

    function recharge_online_save() {
        $amount = I('amount');
        $params = new \Org\Su\Params();
        $params->addParam('siteID', 'number', '用户账户无效!');
        $params->addParam('amount', 'number', '充值金额必须填写!');
        if ($amount && is_numeric($amount) && $amount < 0) {
            $params->addError('充值金额不能小于0!');
        }
        if ($params->hasError()) {
            $this->ajaxMsg(false, $params->getError());
        }
        $siteID = I('siteID');
        $params->clearParams();
        $params->addData('pflt_amount', $amount);
        $params->addData('pint_siteID', $siteID);
        $params->addData('pstr_origin', '在线支付');
        $params->query_procedure('proc_recharge');
        M()->clear_table_caching(array('tuser_member', 'tfinance_recharge'));
        BaseController::add_user_event_log('充值', "充值成功, 租户ID:$siteID, 金额: $amount, 支付方式: 在线支付", true);
        $this->ajaxMsg(true, '充值成功!', AddonU('Recharge/index'));
    }

}
